/* 
Scenic Graphics Library
Copyright (C) 2007 Jouni Tulkki

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USApackage scenic;
*/

package scenic;

import java.util.Iterator;
import java.util.LinkedList;
import java.util.ListIterator;
import java.util.NoSuchElementException;

/**
 * This class is used for creating color slides in gradients.
 * The gradient color is defined using two or more nodes. Each
 * node has a position and a color. The colors between two
 * consecutive nodes are linearely interpolated to produce
 * a smooth color slide.
 */
public class LinearColorFunction implements ColorFunction {
	private static class Node {
		public double position;
		public ScenicColor color;
		
		public Node(double position, ScenicColor color) {
			this.position = position;
			this.color = color;
		}
	};
	
	LinkedList<Node> nodes = new LinkedList<Node>();
	
	/**
	 * Constructs a LinearColorFunction object.
	 *
	 */
	public LinearColorFunction() {
	}

	/**
	 * Adds a new node to this object.
	 * 
	 * @param position the position of the node.
	 * @param color the color of the node.
	 */
	public void addNode(double position, ScenicColor color) {
		ListIterator<Node> itr = nodes.listIterator();
		Node newNode = new Node(position, color);
		
		while(itr.hasNext()) {
			Node node = (Node)itr.next();
			if(node.position == position) {
				itr.remove();
				itr.add(newNode);
				return;
			}
			if(node.position >= position)
			{
				itr.add(newNode);
				return;
			}
		}
		itr.add(newNode);
	}
	
	public ScenicColor value(double x) {
		Node prevNode = null;
		Node node = null;
		Iterator itr = nodes.iterator();
		
		while(itr.hasNext()) {
			prevNode = node;
			node = (Node)itr.next();
			if(node.position > x)
				break;
		}
		
		if(prevNode == null || node == null)
			return new ScenicColor(0.0f, 0.0f, 0.0f, 0.0f);
		
		if(prevNode == null)
			return node.color;
		if(node == null)
			return prevNode.color;
		if(prevNode.position == node.position)
			return prevNode.color;
		
		double v = (x - prevNode.position) / 
			(node.position - prevNode.position);

		return ScenicColor.interpolate(prevNode.color, node.color, (float)v);
	}
	
	public double min() {
		try {
			return ((Node)nodes.getFirst()).position;
		} catch(NoSuchElementException e) {
			return 0.0;
		}
	}
	public double max() {
		try {
			return ((Node)nodes.getLast()).position;
		} catch(NoSuchElementException e) {
			return 0.0;
		}
	}
}
